package com.fivefu.core.hik.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.entity.DbHikTree;
import com.fivefu.core.hik.mapper.DbHikTreeMapper;
import com.fivefu.core.hik.services.DbHikTreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 海康树结构 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-01 16:32:36
 */
@Service
public class DbHikTreeServiceImpl extends ServiceImpl<DbHikTreeMapper, DbHikTree> implements DbHikTreeService {

    /**
     * 通过编号查询树编码
     * @param treeCode
     * @return
     */
    public DbHikTree queryTreeByCode(String treeCode){
        QueryWrapper<DbHikTree> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("tree_code", treeCode);
        DbHikTree tree = baseMapper.selectOne(wrapper);
        return tree;
    }

    /**
     * 编辑树结构信息
     * @param tree
     */
    @Override
    public Long editHikTree(DbHikTree tree){
        LocalDateTime time = LocalDateTime.now();
        if (StrUtils.isNotNull(tree.getCapabilitySet())){

        }
        if (StrUtils.isNotNull(tree.getIntelligentSet())){

        }
        tree.setUpdatedTime(time);
        DbHikTree oldTree = queryTreeByCode(tree.getTreeCode());
        if (oldTree != null){
            tree.setId(oldTree.getId());
            baseMapper.updateById(tree);
        }else{
            tree.setUpdatedTime(time);
            tree.setIsDelete(0);
            baseMapper.insert(tree);
        }
        return tree.getId();
    }

    /**
     * 删除多余树结构
     * @param retainIDs 保留树结构编号
     */
    @Override
    public void deletOtherHikTree(List<Long> retainIDs){
        QueryWrapper<DbHikTree> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (retainIDs != null && !retainIDs.isEmpty()) {
            wrapper.in("id", retainIDs);
        }

        DbHikTree tree = new DbHikTree();
        tree.setIsDelete(1);
        tree.setUpdatedTime(LocalDateTime.now());
        baseMapper.update(tree, wrapper);
    }

}
